package com.fzj.example.thread;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * ClassName: Producer
 * date: 2019/10/24 15:01
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class Producer implements Runnable {

    private Queue<Car> queue;


    public Producer(Queue<Car> queue){
        this.queue=queue;
    }

    @Override
    public void run() {
        while (true){
            try {
                add();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void add() throws InterruptedException {
        synchronized (queue){
            if(queue.size()>=500){
                queue.wait();
                System.out.println("wait");
            }else{
                queue.add(new Car());
            }
        }
    }
}
